/**
 * 
 */
package org.xlab.baseframework.conf;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Session;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import org.xlab.baseframework.common.entity.PasswordAuthenticator;

/**
 * @author MaKuBEX
 * 
 */
@Configuration
public class MailConfiguration {
	@Bean(autowire = Autowire.BY_NAME, name = "bpf_mailSender")
	public JavaMailSender javaMailSender() {
		JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
		javaMailSender.setHost(System.getProperty("mail.host"));
		javaMailSender
				.setPort(Integer.parseInt(System.getProperty("mail.port")));
		javaMailSender.setSession(mailSession());
		return javaMailSender;
	}

	@Bean(autowire = Autowire.BY_NAME, name = "bpf_mailSession")
	public Session mailSession() {
		Session session = Session.getInstance(System.getProperties(),
				passwordAuthenticator());
		session.setDebug(Boolean.getBoolean(System.getProperty("mail.debug")));
		return session;
	}

	@Bean(autowire = Autowire.BY_NAME, name = "bpf_passwordAuthentication")
	public Authenticator passwordAuthenticator() {
		return new PasswordAuthenticator(System.getProperty("mail.username"),
				System.getProperty("mail.password"));
	}

	@Bean(autowire = Autowire.BY_NAME, name = "bpf_velocityEngine")
	public VelocityEngineFactoryBean velocityEngine() {
		Properties props = new Properties();
		props.setProperty("resource.loader", "class");
		props.setProperty("class.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		VelocityEngineFactoryBean velocityEngine = new VelocityEngineFactoryBean();
		velocityEngine.setVelocityProperties(props);
		return velocityEngine;
	}
}
